package pers.lmd.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import pers.lmd.convert.StudentConvert;
import pers.lmd.domain.entity.Student;
import pers.lmd.domain.vo.StudentStatusVO;
import pers.lmd.domain.vo.StudentVO;
import pers.lmd.mapper.StudentMapper;
import pers.lmd.service.IStudentService;

/**
 * @author 10149
 * @description 针对表【student】的数据库操作Service实现
 * @createDate 2023-04-11 23:50:31
 */
@RequiredArgsConstructor
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements IStudentService {

    @Override
    public String getAvatar() {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        Long id = StpUtil.getLoginIdAsLong();
        wrapper.select(Student::getAvatar).eq(Student::getId, id);
        return getOne(wrapper).getAvatar();
    }

    @Override
    public String getPicture() {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        Long id = StpUtil.getLoginIdAsLong();
        wrapper.select(Student::getPicture).eq(Student::getId, id);
        return getOne(wrapper).getPicture();
    }

    @Override
    public StudentVO getInfo() {
        Long id = StpUtil.getLoginIdAsLong();
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getId, id);
        Student student = getOne(wrapper);
        StudentVO studentVO = StudentConvert.INSTANCES.toStudentVO(student);
        return studentVO;
    }

    @Override
    public StudentStatusVO getStatus() {
        Long tid = StpUtil.getLoginIdAsLong();

        LambdaQueryWrapper<Student> negativeWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Student> warningWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Student> positiveWrapper = new LambdaQueryWrapper<>();

        negativeWrapper.eq(Student::getTid, tid).eq(Student::getStatus, 0);
        Long negativeCount = count(negativeWrapper);
        warningWrapper.eq(Student::getTid, tid).eq(Student::getStatus, 1);
        Long warningCount = count(warningWrapper);
        positiveWrapper.eq(Student::getTid, tid).eq(Student::getStatus, 2);
        Long positiveCount = count(positiveWrapper);

        StudentStatusVO studentStatusVO = new StudentStatusVO();
        studentStatusVO.setNegative(negativeCount);
        studentStatusVO.setWarning(warningCount);
        studentStatusVO.setPositive(positiveCount);
        return studentStatusVO;
    }
}




